﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.CustomerManage
{
    public partial class CustomerShow : Common.PageBase
    {
        UserLoginInfo _userLoginInfo;
        T_CUSTOMER_BLL bll = new T_CUSTOMER_BLL();
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();
        T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        T_LOCATIONINFORMATION_BLL liBll = new T_LOCATIONINFORMATION_BLL();
        T_SPECIALLIST_BLL slBll = new T_SPECIALLIST_BLL();
        T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        T_CUSTOMERTELINDEX_BLL ctBll = new T_CUSTOMERTELINDEX_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);
            //PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);
            //PageHelper.SetPopCalender(this.Page, txtFTIMESTAMP);
            PageHelper.SetPopCalender(this.Page, txtBIRTHDATE);
            PageHelper.SetPopCalender(this.Page, txtBECOMEDATE);
            //UserLoginInfo.SetButtonConfirm(btnNormalList);
            //UserLoginInfo.SetButtonConfirm(btnRedList);
            //UserLoginInfo.SetButtonConfirm(btnBlackList);
            //UserLoginInfo.SetButtonConfirm(btnOK);

        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBUSINESSDIVISIONIDBinding();
                ddlDEALERIDBinding();
                ddlCUSTOMERBASEBinding();
                ddlCUSTOMERTYPEBinding();
                ddlCUSTOMERLEVELBinding();
                ddlSPECIALTYPEBinding();
                ddlSTATUSBinding();
                ddlBIGCUSTOMERTYPEBinding();
                ddlFIRSTCONTACTTYPEBinding();


                pageDataInit();
            }
        }


        #region 页面初始化数据绑定

        private void ddlFIRSTCONTACTTYPEBinding()
        {
            ddlFIRSTCONTACTTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("FirstContactType"));
            ddlFIRSTCONTACTTYPE.DataTextField = "DictValue";
            ddlFIRSTCONTACTTYPE.DataValueField = "DictKey";
            ddlFIRSTCONTACTTYPE.DataBind();
        }

        /// <summary>
        /// DDLs the BUSINESSDIVISIONID binding.
        /// </summary>
        private void ddlBUSINESSDIVISIONIDBinding()
        {
            ddlBUSINESSDIVISIONID.DataSource = bdBll.GetCacheList(this.Page, String.Format(" and Status = {0} ", ((int)Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONID.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONID.DataValueField = "BUSINESSDIVISIONID";
            ddlBUSINESSDIVISIONID.DataBind();

        }


        /// <summary>
        /// DDLs the CUSTOMERBASE binding.
        /// </summary>
        private void ddlCUSTOMERBASEBinding()
        {
            ddlCUSTOMERBASE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerBase"));
            ddlCUSTOMERBASE.DataTextField = "DictValue";
            ddlCUSTOMERBASE.DataValueField = "DictKey";
            ddlCUSTOMERBASE.DataBind();
            ddlCUSTOMERBASE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the CUSTOMERTYPE binding.
        /// </summary>
        private void ddlCUSTOMERTYPEBinding()
        {
            ddlCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerType"));
            ddlCUSTOMERTYPE.DataTextField = "DictValue";
            ddlCUSTOMERTYPE.DataValueField = "DictKey";
            ddlCUSTOMERTYPE.DataBind();
            ddlCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the CUSTOMERLEVEL binding.
        /// </summary>
        private void ddlCUSTOMERLEVELBinding()
        {
            ddlCUSTOMERLEVEL.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerLevel"));
            ddlCUSTOMERLEVEL.DataTextField = "DictValue";
            ddlCUSTOMERLEVEL.DataValueField = "DictKey";
            ddlCUSTOMERLEVEL.DataBind();
            ddlCUSTOMERLEVEL.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the BIGCUSTOMERTYPE binding.
        /// </summary>
        private void ddlBIGCUSTOMERTYPEBinding()
        {
            ddlBIGCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("BigCustomerType"));
            ddlBIGCUSTOMERTYPE.DataTextField = "DictValue";
            ddlBIGCUSTOMERTYPE.DataValueField = "DictKey";
            ddlBIGCUSTOMERTYPE.DataBind();
            ddlBIGCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));

        }
        /// <summary>
        /// DDLs the STATUS binding.
        /// </summary>
        private void ddlSTATUSBinding()
        {
            EnumHelper.FillListControl(ddlSTATUS, typeof(CustomerStatus));

        }
        /// <summary>
        /// DDLs the SPECIALTYPE binding.
        /// </summary>
        private void ddlSPECIALTYPEBinding()
        {
            ddlSPECIALTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlSPECIALTYPE, typeof(CustomerSpecialType));
        }

        /// <summary>
        /// DDLs the COUNTRY binding.
        /// </summary>
        private void ddlCOUNTRYBinding()
        {

            ddlCOUNTRY.DataSource = liBll.GetCacheListByParentCode(this.Page, "");
            ddlCOUNTRY.DataTextField = "NAME";
            ddlCOUNTRY.DataValueField = "CODE";
            ddlCOUNTRY.DataBind();
            ddlCOUNTRY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the PROVINCE binding.
        /// </summary>
        private void ddlPROVINCEBinding()
        {
            ddlPROVINCE.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlCOUNTRY.SelectedValue.Trim());
            ddlPROVINCE.DataTextField = "NAME";
            ddlPROVINCE.DataValueField = "CODE";
            ddlPROVINCE.DataBind();
            ddlPROVINCE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the PREFECTURECITY binding.
        /// </summary>
        private void ddlPREFECTURECITYBinding()
        {
            ddlPREFECTURECITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlPROVINCE.SelectedValue.Trim());
            ddlPREFECTURECITY.DataTextField = "NAME";
            ddlPREFECTURECITY.DataValueField = "CODE";
            ddlPREFECTURECITY.DataBind();
            ddlPREFECTURECITY.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// DDLs the CITY binding.
        /// </summary>
        private void ddlCITYBinding()
        {
            ddlCITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlPREFECTURECITY.SelectedValue.Trim());
            ddlCITY.DataTextField = "NAME";
            ddlCITY.DataValueField = "CODE";
            ddlCITY.DataBind();
            ddlCITY.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the VILLAGETOWN binding.
        /// </summary>
        private void ddlVILLAGETOWNBinding()
        {
            //ddlVILLAGETOWN.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlCITY.SelectedValue.Trim());
            //ddlVILLAGETOWN.DataTextField = "NAME";
            //ddlVILLAGETOWN.DataValueField = "CODE";
            //ddlVILLAGETOWN.DataBind();
            //ddlVILLAGETOWN.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 经销商编号 下拉列表数据绑定
        /// </summary>
        private void ddlDEALERIDBinding()
        {
            //string businessDivisionCode = string.Empty;
            //T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            //if(bdEntity != null)
            //{
            //    businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            //}
            //ddlDEALERID.DataSource = dealerBll.GetList(string.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode));
            //ddlDEALERID.DataTextField = "DEALERNAME";
            //ddlDEALERID.DataValueField = "DEALERID";
            //ddlDEALERID.DataBind();
            //ddlDEALERID.Items.Insert(0, new ListItem("", ""));
        }

        #endregion

        /// <summary>
        /// Pages the data init.
        /// </summary>
        private void pageDataInit()
        {
            string _customerid = String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]) ? "" : Request.QueryString["CUSTOMERID"].Trim();

            if (!String.IsNullOrEmpty(_customerid))
            {
                T_CUSTOMER_Entity entity = bll.GetEntity("CUSTOMERID", _customerid);

                #region 客户地区级联
                ddlCOUNTRYBinding();
                ddlCOUNTRY.SelectedValue = entity.COUNTRY;
                ddlCOUNTRY_SelectedIndexChanged(null, null);
                ddlPROVINCEBinding();
                ddlPROVINCE.SelectedValue = entity.PROVINCE;
                ddlPROVINCE_SelectedIndexChanged(null, null);
                ddlPREFECTURECITYBinding();
                ddlPREFECTURECITY.SelectedValue = entity.PREFECTURECITY;
                ddlPREFECTURECITY_SelectedIndexChanged(null, null);
                ddlCITYBinding();
                ddlCITY.SelectedValue = entity.CITY;
                ddlCITY_SelectedIndexChanged(null, null);
                ddlVILLAGETOWNBinding();
                //ddlVILLAGETOWN.SelectedValue = entity.VILLAGETOWN;
                #endregion

                PageHelper.SetControlValue<T_CUSTOMER_Entity>(this.Page, entity);
                txtBIRTHDATE.Text = (entity.BIRTHDATE == null) ? "" : entity.BIRTHDATE.Value.ToString("yyyy-MM-dd");
                txtBECOMEDATE.Text = (entity.BECOMEDATE == null) ? "" : entity.BECOMEDATE.Value.ToString("yyyy-MM-dd");
                ddlBUSINESSDIVISIONIDBinding();
                ddlBUSINESSDIVISIONID.SelectedValue = entity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID_SelectedIndexChanged(null, null);
                ddlDEALERIDBinding();
                //ddlDEALERID.SelectedValue = entity.DEALERID.ToString();




                ddlSPECIALTYPE.Enabled = false;
                SetListButtonVisible(entity.SPECIALTYPE);
                lblTitle.Text = "客户信息修改";

                if (!UserLoginInfo.IsAdministrator())
                {
                    ddlBUSINESSDIVISIONID.Enabled = false;
                }
            }
            else
            {
                txtCUSTOMERID.Text = bll.GenerateCustomerID(_userLoginInfo);
                txtCUSTOMERCODE.Text = txtCUSTOMERID.Text;
                lblTitle.Text = "客户信息添加";

                //btnBlackList.Visible = false;
                //btnRedList.Visible = false;
                //btnNormalList.Visible = false;
                #region 客户地区级联
                ddlCOUNTRYBinding();
                ddlCOUNTRY_SelectedIndexChanged(null, null);
                ddlPROVINCEBinding();
                ddlPROVINCE_SelectedIndexChanged(null, null);
                ddlPREFECTURECITYBinding();
                ddlPREFECTURECITY_SelectedIndexChanged(null, null);
                ddlCITYBinding();
                ddlCITY_SelectedIndexChanged(null, null);
                ddlVILLAGETOWNBinding();
                #endregion

                if (!UserLoginInfo.IsAdministrator())
                {
                    ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                    ddlBUSINESSDIVISIONID_SelectedIndexChanged(null, null);
                    ddlBUSINESSDIVISIONID.Enabled = false;
                }
                txtBECOMEDATE.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
            txtCREATEPEOPLE.Enabled = false;
            //txtCREATEPEOPLEID.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            //txtLASTMODIFYPEOPLEID.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;

            txtCUSTOMERID.ReadOnly = true;



        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _customerid = String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]) ? "" : Request.QueryString["CUSTOMERID"].Trim();
            T_CUSTOMER_Entity entity = bll.GetEntity("CUSTOMERID", _customerid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_CUSTOMER_Entity>(this.Page, ref entity);
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                //事业部
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                //经销商
                //T_DEALER_Entity dealerEntity = dealerBll.GetCache_T_DEALER_EntityByKey(this.Page, StringHelper.ToInt(ddlDEALERID.SelectedValue.Trim()));
                //if (dealerEntity != null)
                //{
                //    entity.DEALERCODE = dealerEntity.DEALERCODE;
                //    entity.DEALERNAME = dealerEntity.DEALERNAME;
                //}


                try
                {
                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        ctBll.UpdateCustomerTelIndex(entity);
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {

                    PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_CUSTOMER_Entity();

                PageHelper.GetControlValue<T_CUSTOMER_Entity>(this.Page, ref entity);
                entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.CREATETIME = DateTime.Now;
                entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
                entity.LASTMODIFYTIME = DateTime.Now;
                //事业部
                T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
                if (bdEntity != null)
                {
                    entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
                }
                //经销商
                //T_DEALER_Entity dealerEntity = dealerBll.GetCache_T_DEALER_EntityByKey(this.Page, StringHelper.ToInt(ddlDEALERID.SelectedValue.Trim()));
                //if (dealerEntity != null)
                //{
                //    entity.DEALERCODE = dealerEntity.DEALERCODE;
                //    entity.DEALERNAME = dealerEntity.DEALERNAME;
                //}

                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        ctBll.UpdateCustomerTelIndex(entity);
                        CustomerSpecialType type = (CustomerSpecialType)Enum.Parse(typeof(CustomerSpecialType), ddlSPECIALTYPE.SelectedValue);
                        slBll.UpdateSpecialList(entity, type, _userLoginInfo, entity.HOMEPHONE.Trim());
                        PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失改！");
                    }

                }
                catch (Exception ex)
                {

                    PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the btnList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnList_Click(object sender, EventArgs e)
        {
            CustomerSpecialType type = CustomerSpecialType.正常名单;
            Button btn = sender as Button;
            if (btn != null)
            {
                switch (btn.CommandName.ToLower())
                {
                    case "blacklist":
                        {
                            type = CustomerSpecialType.黑名单;
                        }
                        break;
                    case "redlist":
                        {
                            type = CustomerSpecialType.红名单;
                        }
                        break;
                    case "normallist":
                        {
                            type = CustomerSpecialType.正常名单;
                        }
                        break;
                }

                string _customerid = String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]) ? "" : Request.QueryString["CUSTOMERID"].Trim();
                T_CUSTOMER_Entity entity = bll.GetEntity("CUSTOMERID", _customerid);
                if (entity != null)
                {
                    entity.SPECIALTYPE = ((int)type).ToString();
                    bll.Update(entity);
                    bool rel = slBll.UpdateSpecialList(entity, type, _userLoginInfo, entity.HOMEPHONE.Trim());
                    if (rel)
                    {
                        PageHelper.ShowMessage(this.Page, string.Format("转{0}成功！", type.ToString()), UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(string.Format("转{0}失败！", type.ToString()));
                    }
                }
            }
        }

        #region DDL  事件
        protected void ddlCOUNTRY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPROVINCEBinding();

            ddlPREFECTURECITYBinding();
            ddlCITYBinding();
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;

        }

        protected void ddlPROVINCE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPREFECTURECITYBinding();
            ddlCITYBinding();
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;
            txtADDRESS.Text += ddlPROVINCE.SelectedItem.Text;
        }

        protected void ddlPREFECTURECITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCITYBinding();
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;
            txtADDRESS.Text += ddlPROVINCE.SelectedItem.Text;
            txtADDRESS.Text += ddlPREFECTURECITY.SelectedItem.Text;
        }

        protected void ddlCITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;
            txtADDRESS.Text += ddlPROVINCE.SelectedItem.Text;
            txtADDRESS.Text += ddlPREFECTURECITY.SelectedItem.Text;
            txtADDRESS.Text += ddlCITY.SelectedItem.Text;
        }

        protected void ddlBUSINESSDIVISIONID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDEALERIDBinding();
        }
        #endregion

        /// <summary>
        /// Sets the list button visible.
        /// </summary>
        /// <param name="customerSpecialType">Type of the customer special.</param>
        protected void SetListButtonVisible(string customerSpecialType)
        {
            CustomerSpecialType type = CustomerSpecialType.正常名单;
            try
            {
                type = (CustomerSpecialType)Enum.Parse(typeof(CustomerSpecialType), customerSpecialType);
            }
            catch
            {

            }
            //switch (type)
            //{
            //    case CustomerSpecialType.正常名单:
            //        {
            //            btnNormalList.Visible = false;
            //        }
            //        break;
            //    case CustomerSpecialType.黑名单:
            //        {
            //            btnBlackList.Visible = false;
            //        }
            //        break;
            //    case CustomerSpecialType.红名单:
            //        {
            //            btnRedList.Visible = false;
            //        }
            //        break;
            //}

        }

        

    }
}
